package driving;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.generate.to.ArticleTo;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.model.po.Article;
import com.g2room.driving.util.DbUtil;
import com.g2room.driving.util.HtmlUtil;
import com.g2room.driving.util.ImageUtil;

public class HandleContentByJsonup {
	private static final int type_qg = 1;
	private static final int type_kc = 2;
	static Map<String, String> oldImgMap = null;//getOldImgs();
	public static void main(String[] args) {
		// 去掉注释、style和script标签
//		handleCommonTag();
//		handleQqTag();
//		handleJxydtTag();
//		handleKcjqTag();
//		handleCzzjTag();
//		handleQczjTag();
		
//		updateNrxTag();
		
//		findHotArti();
//		handlImgs();
//		handlThumbImgs();
		

		handlQg();
		
//		DbUtil.closeConn();
	}

	
	public static void handlQg() {
		List<Article> articles = getDetails("class=\"endPageNum\"", null, type_qg);
		System.out.println(articles.size());
		int i = 0;
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "endPageNum");
				i++;
				els.remove();
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
    	System.out.println("共处理数据：" + i);
	}
	
	/**
	 * 从内容页面获取缩略图
	 */
	public static void handlThumbImgs() {
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			PreparedStatement updateSt = connection.prepareStatement("update d_article_grap set thumb=? where id=?");
			String sql="select id,content from d_article_grap where thumb is null";
			ResultSet rs = statement.executeQuery(sql);
			String mainImg = "";
			while (rs.next()) {
				int id = rs.getInt("id");
				String content = rs.getString("content");
				ArticleTo at = new ArticleTo();
				at = getImgFromContent(content, at, 1.5, 0.4, 100);
				mainImg = at.getMainImg();
				if(null == mainImg || mainImg.length() == 0) continue;
				// 遍历处理sql语句
				try {
					updateSt.setString(1, mainImg);
					updateSt.setInt(2, id);
					updateSt.execute();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			rs.close();
			updateSt.close();
			statement.close();
			DbUtil.closeConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 从内容中提取出符合要求的图片
	 * @param content
	 * @param articleTo
	 * @param pr
	 * @param fluc
	 * @param w
	 * @return
	 */
	private static ArticleTo getImgFromContent(String content, ArticleTo articleTo, double pr, double fluc, int w) {
		// 提取src值
		List<String> imgSrcs = ImageUtil.getImgSrc(content);
		if(imgSrcs.isEmpty()) {
			return articleTo;
		}
		
		
		for(String imgSrc : imgSrcs) {
			System.out.println(imgSrc);
			if(!oldImgMap.containsKey(imgSrc)) continue;
			String localSrc = oldImgMap.get(imgSrc);
			try {
				double imgPr = getPr(localSrc, articleTo);
		        if(pr > 0 && rangeInDefined(imgPr, pr-fluc, pr+fluc) && articleTo.getImgWidth() >= w) {
		        	articleTo.setMainImg(imgSrc);
		        	break;
		        } else if(articleTo.getImgWidth() >= w){
		        	articleTo.setMainImg(imgSrc);
		        }
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		return articleTo;
	}
	/**
	 * 判断是否在范围内，在则返回true
	 * @param current	当前数据
	 * @param min 最小
	 * @param max	最大
	 * @return
	 */
	public static boolean rangeInDefined(double current, double min, double max) {
		return Math.max(min, current) == Math.min(current, max);
	}
	

	/**
	 * 获取图片尺寸比率
	 * @param imgSrc
	 * @return
	 * @throws Exception
	 */
	private static double getPr(String imgSrc, ArticleTo articleTo) throws Exception {
		// 替换URL，从本地获取图片
//		imgSrc = imgSrc.replace("http://www.wine22.com/images/", localImgDir).replace("http://www.g2room.com/images/", localImgDir);
		File f = new File(imgSrc);
		System.out.println("本地图片："+imgSrc);
		double pr = 0;
	    try {
	    	BufferedImage sourceImg =ImageIO.read(new FileInputStream(f));
	    	if(null != sourceImg) {
	    		articleTo.setImgWidth(sourceImg.getWidth());
	    		pr = Double.valueOf(articleTo.getImgWidth()) / sourceImg.getHeight();
	    	}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
        return pr;
	}
	/**
	 * 获取所有图片的content里面的地址
	 * @return
	 */
	public static Map<String, String> getOldImgs() {
		Map<String, String> map = new HashMap<String, String>();
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			String sql="select id,oldSrc,localSrc from d_images where type=0";
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				map.put(rs.getString("oldSrc"), rs.getString("localSrc"));
			}
			rs.close();
			statement.close();
			DbUtil.closeConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 处理之前的updateSql到images表
	 */
	public static void handlImgs() {
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			PreparedStatement updateSt = connection.prepareStatement("insert into d_images(oldSrc, localSrc,type) values(?,?,?)");
			String sql="select id,updateSql from d_images_update";
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				String updateSqlStr = rs.getString("updateSql");
				//update d_article_grap set content=replace(content,'/uploadfile/201.jpg','E:/高绪国/资料/images/qgsh/sourceImg/20160509/nrj/4076850672531dM4qXJ.jpg');
				String oldSrc = updateSqlStr.substring(updateSqlStr.indexOf("'") + 1, updateSqlStr.indexOf("','"));
				String localSrc = updateSqlStr.substring(updateSqlStr.indexOf("','")+3, updateSqlStr.lastIndexOf("'"));
				// 遍历处理sql语句
				try {
					updateSt.setString(1, oldSrc);
					updateSt.setString(2, localSrc);
					updateSt.setInt(3, 0);
					updateSt.execute();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
			rs.close();
			updateSt.close();
			statement.close();
			DbUtil.closeConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void updateNrxTag() {
		List<Article> articles = getSourceUrl(null, "女人巷");
		for(Article a: articles) {
			Document doc = GenericGrap.getDoc(a.getSourceUrl(), "gb2312");
			Element tagEl = doc.getElementsByAttributeValue("class", "aList-label").get(0);
			if(null != tagEl && tagEl.text().trim().length() > 0)
            updateTag(a.getId(), tagEl.text().trim().replaceAll(" ", ","));
		}
	}
	
	public static void handleQczjTag() {
		List<Article> articles = getDetails(null, "汽车之家");
		System.out.println("汽车之家===="+articles.size());
		int i = 0;
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "conck");
				i++;
				els.remove();
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
    	System.out.println("共处理数据：" + i);
	}
	
	/**
	 * 查找可能的热门页面
	 */
	public static void findHotArti() {
		System.out.println("----------------");
		List<Article> articles = getDetails(null, null);
		StringBuffer ids = new StringBuffer();
		int i =0;
		for(Article arti : articles) {
			List<String> imgSrcs = ImageUtil.getImgSrc(arti.getContent());
			if(imgSrcs.size()>3 && arti.getContent().length()>1500) {
				if(i % 20 == 0) System.out.println();
				ids.append(arti.getId() + ",");
				i++;
			}
		}
		System.out.println("----------------"+ids.length());
		System.out.println(ids.toString());
	}
	
	public static void handleCommonTag() { 
		List<Article> articles = getDetails(null, null);
		System.out.println("处理的文章数："+articles.size());

		for(Article arti : articles) {
			try{
				// 删除注释
				String html = HtmlUtil.delNoteTag(arti.getContent());
				// 删除js和style
				html = HtmlUtil.delScriptAndStyleTag(html);
				Document doc = Jsoup.parse(html);
				// 处理所有a标签
				Elements aels = doc.getElementsByTag("a");
				if(null != aels) {
					for(Element ele : aels) {
						String href = ele.attr("href");
						if(href.contains("kaichejiqiao.cn")) continue;
						ele.after(HtmlUtil.delATag(ele.html()));
						ele.remove();
					}
				}
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	e.printStackTrace();
	        }
		}
	}
	public static void handleQqTag() {
		List<Article> articles = getDetails(null, "腾讯汽车");
		System.out.println("腾讯汽车===="+articles.size());
		int i = 0;
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "article");
				if(null == els || els.size() == 0) continue;
				// 获取第一层class="acticle"
				Element acticleDiv = els.get(0);
				els = acticleDiv.children();
				for (Element el : els) {
					// 获取div下第一层元素
					String className = el.attr("class");
					String tagName = el.tagName();
					String id = el.attr("id");
					if((tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("rv-root-v2 rv-js-root")) 
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("relInfo clearfix") && id.equalsIgnoreCase("relInfo"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("relativeVideo clearfix") && id.equalsIgnoreCase("invideocon"))
							|| (tagName.equalsIgnoreCase("div") && id.equalsIgnoreCase("ch"))
							|| (tagName.equalsIgnoreCase("span") && id.equalsIgnoreCase("focusHub"))
							|| (tagName.equalsIgnoreCase("span") && id.equalsIgnoreCase("goRead"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("tJieHot_"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("BJCZdc"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("rich_wb"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("rich"))
							|| (tagName.equalsIgnoreCase("p") && el.text().trim().length() == 0)
							) {
						i++;
						el.remove();
					}
				}
				// 单独处理
				Element el = doc.getElementById("backqqcom");
				if(null != el) el.remove();
				// 处理所有a标签
				Elements aels = doc.getElementsByTag("a");
				if(null != aels) {
					for(Element ele : aels) {
						String href = ele.attr("href");
						if(href.contains("kaichejiqiao.cn")) continue;
						ele.after(HtmlUtil.delATag(ele.html()));
						ele.remove();
					}
				}
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
    	System.out.println("共处理数据：" + i);
	}
	// 车主之家
	public static void handleCzzjTag() {
		List<Article> articles = getDetails(null, "车主之家");
		System.out.println("车主之家====="+articles.size());
		int i = 0;
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "article");
				if(null == els || els.size() == 0) continue;
				Element acticleDiv = els.get(0);
				els = acticleDiv.children();
				for (Element el : els) {
					// 获取div下第一层元素
					String className = el.attr("class");
					String tagName = el.tagName();
					String style = el.attr("style");
					String cellspacing = el.attr("cellspacing");
					if((tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("share_box") && style.equalsIgnoreCase("margin-top:20px;overflow:visible;")) 
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("new_related"))
							|| (tagName.equalsIgnoreCase("span") && className.equalsIgnoreCase("news_related"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("nav_box"))
							|| (tagName.equalsIgnoreCase("div") && style.equalsIgnoreCase("position:relative;"))
							|| (tagName.equalsIgnoreCase("table") && style.equalsIgnoreCase("width:500px;background-color:#d3deeb;margin-top:0;") && cellspacing.equals("1"))
							|| (tagName.equalsIgnoreCase("div") && className.equalsIgnoreCase("page") && style.equalsIgnoreCase("text-align:center;padding:20px 0;"))) {
						i++;
						el.remove();
					}
					
					if(tagName.equalsIgnoreCase("p")) {
						Elements elems = el.children();
						for (Element e : elems) {
							String classNamep = el.attr("class");
							String tagNamep = el.tagName();
							String stylep = el.attr("style");
							if((tagNamep.equalsIgnoreCase("span") && classNamep.equalsIgnoreCase("news_related"))
									|| (tagNamep.equalsIgnoreCase("span") && stylep.equalsIgnoreCase("padding:10px 0;display:block;border-top:#ccc 1px solid;line-height:24px;border-bottom:#ccc 1px solid;overflow:hidden;"))
									) {
								e.remove();
							}
						}
					}
				}
				// 单独处理相关阅读推荐
				els = doc.getElementsByAttributeValue("style", "padding:10px 0;display:block;border-top:#CCC 1px solid;line-height:24px;border-bottom:#CCC 1px solid;overflow:hidden;");
				if(null != els && els.size() > 0) {
					Element element = els.get(0);
					element.parent().remove();
				}
				// 另外一种样式的阅读推荐
				els = doc.getElementsByAttributeValue("class", "news_related");
				els.remove();
				els = doc.getElementsByAttributeValue("class", "fontsize");
				els.remove();
				els = doc.getElementsByTag("strong");
				for(Element el : els) {
					if(el.text().contains("相关推荐阅读")) {
						el.parent().remove();
					}
				}
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
		System.out.println("共处理数据：" + i);
	}
	
	public static void handleJxydtTag() {
		List<Article> articles = getDetails(null, "驾校一点通");
		System.out.println("驾校一点通===="+articles.size());
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "article");
				if(null == els || els.size() == 0) continue;
				// 获取第一层class="acticle"
				Element acticleDiv = els.get(0);
				els = acticleDiv.children();
				for (Element el : els) {
					// 获取div下第一层元素
					String tagName = el.tagName();
					if((tagName.equalsIgnoreCase("p") && el.text().trim().length() == 0)
							) {
						el.remove();
					}
				}
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
	}

	public static void handleKcjqTag() {
		List<Article> articles = getDetails(null, "开车技巧网");
		System.out.println("开车技巧网===="+articles.size());
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				Elements els = doc.getElementsByAttributeValue("class", "pagecontent");
				if(null == els || els.size() == 0) continue;
				// 获取第一层class="acticle"
				els.get(0).remove();
				Elements divEls = doc.getElementsByTag("div");
				for (Element el : divEls) {
					// 获取div下第一层元素
					String text = el.text();
					if(text.trim().length() == 0) el.remove();
				}
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
	}
	
	public static String handleSpace(String str) {
		return str.replaceAll("&nbsp;", " ");
	}
	
	public static void updateContent(long id, String content) {
		update(id, content, "content", type_qg);
	}
	

	public static void updateTag(long id, String tags) {
		update(id, tags, "tag", type_qg);
	}
	

	public static void update(long id, String text, String t, int type) {
		try {
			Connection connection = DbUtil.getQgshConn();
			PreparedStatement statement = connection.prepareStatement("update "+(type == type_qg ? "qg_article_grap" : "d_article_grap") +" set "+t+" =? where id=?");
			statement.setString(1, text);
			statement.setLong(2, id);
			statement.execute();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static List<Article> getDetails(String condition, String source) {
		return getDetails(condition, source, type_kc);
	}

	public static List<Article> getDetails(String condition, String source, int type) {
		List<Article> articles = new ArrayList<Article>();
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			condition = (null == condition) ? condition = "" : "and content like '%"+condition+"%' "; 
			source = (null == source) ? source = "" : "and source='"+source+"' ";
//			String sql = "select id,content from d_article_grap "
//			String sql="select id,category_id, category_code, type_code, type_id,title,content from d_article_grap "
			String sql="select id,title,content,hot from "+(type == type_qg ? "qg_article_grap" : "d_article_grap")
//					+" where hot = 2"
//			String sql = "select id,content from d_article "
					+ " where 1=1 " + condition + source;
//					+ "where id in (574)"
//					+ "where id=6632 limit 1";
					;
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
//				int hot = rs.getInt("hot");
//				if(hot == 1) continue;
				Article article = new Article();
				article.setId(rs.getLong("id"));
				article.setContent(rs.getString("content"));
//				article.setCategoryCode(rs.getString("category_code"));
//				article.setTypeCode(rs.getString("type_code"));
//				if(null == article.getTypeCode() || article.getTypeCode().length()==0) {
//					article.setTypeCode(article.getCategoryCode());
//				}
				articles.add(article);
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	


	public static List<Article> getSourceUrl(String condition, String source) {
		List<Article> articles = new ArrayList<Article>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			condition = (null == condition) ? condition = "" : "and content like '%"+condition+"%' "; 
			source = (null == source) ? source = "" : "and source='"+source+"' ";
			String sql="select id,source_url from d_article_grap where "
					+ "where 1=1 " + condition + source;
					;
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				Article article = new Article();
				article.setId(rs.getLong("id"));
				article.setSourceUrl(rs.getString("source_url"));
				articles.add(article);
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
}
